package org.gcb.core.sandbox;

import org.gcb.core.sandbox.SlotModelEvent;
import org.gcb.core.sandbox.SlotModel;
import java.util.EventListener;
import javax.swing.event.EventListenerList;
import org.gcb.core.sandbox.SlotListener;

/**
 *
 * @author Jason
 */
public abstract class AbstractSlotModel<T> implements SlotModel<T> {
    protected EventListenerList listenerList = new EventListenerList();
    private final T t = null;

    @SuppressWarnings("unchecked")
    public Class<T> getSlotClass() {
	return (Class<T>) t.getClass();
    }

    @Override
    public boolean isEmpty() {
	return true;
    }

    @Override
    public abstract T getItem();

    @Override
    public void setItem(T item){};
//
// Managing Listeners
//

    @Override
    public void addSlotListener(SlotListener l) {
	listenerList.add(SlotListener.class, l);
    }

    @Override
    public void removeSlotListener(SlotListener l) {
	listenerList.add(SlotListener.class, l);
    }

    public SlotListener[] getSlotListeners() {
	return listenerList.getListeners(SlotListener.class);
    }

    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
	return listenerList.getListeners(listenerType);
    }

    //
    // Fire methods
    //

    public void fireSlotDataChanged() {
	fireSlotChanged(new SlotModelEvent(this));
    }

    public void fireSlotSwap() {
	fireSlotChanged(new SlotModelEvent(this, SlotModelEvent.SWAP));
    }

    public void fireSlotEquip() {
	fireSlotChanged(new SlotModelEvent(this, SlotModelEvent.EQUIP));
    }

    public void fireSlotUnequip() {
	fireSlotChanged(new SlotModelEvent(this, SlotModelEvent.UN_EQUIP));
    }

    public void fireSlotChanged(SlotModelEvent sme) {
	for (SlotListener slotListener : listenerList.getListeners(SlotListener.class)) {
	    slotListener.slotChanged(sme);
	}
    }
}
